package com.biye.api.controller.stu_base;

import com.alibaba.fastjson.JSONObject;
import com.base.common.alipay.config.AlipayConfig;
import com.base.common.alipay.util.AlipayUtil;
import com.biye.api.service.MoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/base")
public class MoneyController {

    @Value("${alipay.callback-url:}")  // 加上半角：号，若没有该配置属性，则变量值为"" 【"".equals(contextPath)为true】
    private String callback_root_url;

    @Autowired
    private MoneyService moneyService;

    // 学生账户余额修改：余额充值 -> 引导用户跳转至支付页面，支付成功后“回调”通知auth模块，auth做相应的余额变更逻辑
    @RequestMapping(value = "/pay", method = RequestMethod.GET)
    @ResponseBody
    public void testFundTransfer(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "amount", required = true) String amount, // 充值金额
                                 @RequestParam(value = "stu_id", required = true) String stu_id,
                                 @RequestParam(value = "return_page", required = true) String return_page) throws IOException {
        String callback_page = return_page;
        String callback_interface = "auth/alipay/notifyUU";
        String passback_params = stu_id + "@" + "edu";
        String form = AlipayUtil.sponserPay("余额充值",
                "教务系统用户余额充值",
                amount,
                "30m",
                callback_root_url,
                callback_page,
                callback_interface,
                passback_params);
        if ( !"".equals(form) ){
            String CHARSET = AlipayConfig.CHARSET;
            response.setContentType( "text/html;charset="  + CHARSET);
            response.getWriter().write(form); //直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        }

    }

    // 学生账户余额查询
    @RequestMapping(value = "/queryBalance", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryAccount(@RequestBody Map<String, Object> map) throws IOException {
        return moneyService.queryStuAccount(map);
    }

    // 学生账户余额修改：余额提现
    @RequestMapping(value = "/accountWithdraw", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject withdraw(@RequestBody Map<String, Object> map) throws IOException {
        return moneyService.withdraw(map);
    }

    /*
    produces定义返回数据的处理格式，防止返回数据"中文"变成"???????"
     */
    // 交易记录查询
    @RequestMapping(value = "/queryAccountLog", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String queryLog(@RequestBody Map<String, Object> map) {
        return moneyService.queryLog(map);
    }

}
